package com.leng.common.core;


import com.leng.common.constant.ExceptionEnum;
import com.leng.common.exception.ServiceExceptionWithoutLog;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import java.io.Serializable;
import java.util.List;

/**
 * @author xialin.yan
 * @version V1.0.1
 * @projectName shop
 * @packageName cn.dental360.common.web.base
 * @description 通用结果返回，如果要让swagger能显示返回数据的详细说明，建议返回ApiResult子类并加上相应swagger注解
 * @email xiaolin.yan@dental360.cn
 * @date 2019/10/24 13:38
 **/
public class ApiResult<T> implements Serializable {

    //返回提示信息
    private String msg;
    //返回数据
    private T data;
    //返回状态码
    private Integer code;



    public ApiResult(){
        init(this);
    }


    /**
     * func 校验参数并输出规范的参数提示
     * @param bindingResult
     */
    public static void formValid(BindingResult bindingResult){
        if( bindingResult.getAllErrors().size()==0 ){ return  ; }
        List<ObjectError> errors = bindingResult.getAllErrors();
        StringBuffer errorMsg = new StringBuffer();
        for (ObjectError objectError : errors) {
            errorMsg.append(objectError.getDefaultMessage()+";");
        }
        throw new ServiceExceptionWithoutLog(errorMsg.toString(), ExceptionEnum.PARAM_ERROR.getCode());
    }

//    /**
//     * 无法使用BindingResult自动校验时，使用此方法
//     * @param obj
//     * @param <T>
//     */
//    public static <T> void  formValidManual(T obj){
//        Map<String, String> errors = BeanValidator.validate(obj);
//        StringBuffer errorMsg = new StringBuffer();
//        errors.forEach((key,value)-> {
//            errorMsg.append(key + value + ";");
//        });
//        throw new ServiceExceptionWithoutLog(errorMsg.toString(), ExceptionEnum.PARAM_ERROR.getCode());
//    }


    public static <T> ApiResult success(T data){
        ApiResult result = new ApiResult();
        result.setCode(ApiStatusCode.SUCCESS.getCode());
        result.setMsg("success");
        result.setData(data);
        return result;
    }



    public static ApiResult success( ){
        ApiResult result = new ApiResult();
        init(result);
        return result;
    }

    private static void init(ApiResult result){
        result.setCode(ApiStatusCode.SUCCESS.getCode());
        result.setMsg("success");
    }

    /**
     * 错误返回
     * @param code
     * @param msg 错误描述
     * @return
     */
    public ApiResult error(int code,String msg){
        this.code = code;
        this.msg = msg;
        return this;
    }

    public static ApiResult failed(){
        ApiResult result = new ApiResult();
        result.setCode(-1);
        result.setMsg("操作失败，请重试");
        return result;
    }

    public static ApiResult failed(String msg){
        ApiResult result = new ApiResult();
        result.setCode(ApiStatusCode.SAVE_UPDATE_FAILED.getCode());
        result.setMsg(msg);
        return result;
    }

    public static ApiResult error(ExceptionEnum info){
        ApiResult result = new ApiResult();
        result.setCode(info.getCode());
        result.setMsg(info.getMsg());
        result.setData("");
        return result;
    }


    public static ApiResult error(ApiStatusCode info, Object... infoParts){
        ApiResult result = new ApiResult();
        result.setData("");
        result.setCode(info.getCode());
        if(infoParts!=null &&  infoParts.length>0){
            result.setMsg(String.format(info.getMsg(), infoParts));
        }
        else{
            result.setMsg(info.getMsg());
        }
        return result;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }



    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }
}
